using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
using Keys = Microsoft.Xna.Framework.Input.Keys;

namespace _440_Homework1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        KeyboardState oldState;
        SpriteBatch spriteBatch;
        Player[] players;
        Texture2D land;
        Texture2D water;
        public int size;
        public int height;
        public int width;
        public Board board;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //MessageBox.Show("Board size is " + this.size);
            // TODO: Add your initialization logic here
            oldState = Keyboard.GetState();
            this.initBoard();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            land = Content.Load<Texture2D>("TileSprites/land");
            water = Content.Load<Texture2D>("TileSprites/water");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) || (Keyboard.GetState().GetPressedKeys().Contains(Keys.Escape)))
            {
                this.Exit();
            }

            UpdateInput();

            // TODO: Add your update logic here


            base.Update(gameTime);
        }

        private void UpdateInput()
        {
            KeyboardState newState = Keyboard.GetState();

            // Is the DELETE key down?
            if (newState.IsKeyDown(Keys.Delete))
            {
                // If not down last update, key has just been pressed.
                if (oldState.IsKeyUp(Keys.Delete))
                {
                    initBoard();
                }
            }
            else if (oldState.IsKeyDown(Keys.Delete))
            {
                // Key was down last update, but not down now, so
                // it has just been released.
            }

            // Update saved state.
            oldState = newState;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            // TODO: Add your drawing code here
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            DrawBoard();
            //spriteBatch.Draw(land, landPosition, Color.White);
            //spriteBatch.Draw(water, waterPosition, Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        // TODO: Move this to the Board class

        private void initBoard()
        {
            var settings = new SettingsForm(this);
            settings.ShowDialog();
            board = new Board(this.size);
        }

        private void DrawBoard()
        {
            Tile t;
            float x = 0.0f;
            float y = 0.0f;
            Vector2 tilePosition;

            for (int i = 0; i < board.size; i++)
            {
                for (int j = 0; j < board.size; j++)
                {
                    t = board.tileSet[i, j];
                    tilePosition = new Vector2(x, y);
                    spriteBatch.Draw(Content.Load<Texture2D>(t.tileSprite), tilePosition, Color.White);

                    x += land.Width;
                }
                x = 0.0f;
                y += land.Height;
            }
            graphics.PreferredBackBufferHeight = this.height;
            graphics.PreferredBackBufferWidth = this.width;
            graphics.ApplyChanges();
        }

    }
}
